#' Get AQS data from EPA
#'
#' This function downloads and cleans the Air Quality System (AQS) data from EPA
#' for the FIPS code
#' 
#' @param paramter parameter used to name csv file on EPA website: https://aqs.epa.gov/aqsweb/airdata/download_files.html#Raw
#' @param year year for which data is needed
#' @param FIPS_codes: Federal Information Processing Standard code for location
#' @param parameter_codes paramter code for measure of interest: https://aqs.epa.gov/aqsweb/documents/codetables/parameters.html
#' @param save_file True is source file should be saved
#' 

require(tidyverse)

getHourlyAQS <- function(parameter, year, FIPS_codes, parameter_codes, save_file = FALSE){
  
  # downloading data file from https://aqs.epa.gov
  
  file_link <- paste("https://aqs.epa.gov/aqsweb/airdata/hourly_", parameter, "_", year, ".zip", sep = "")
  
  download.file(file_link, destfile = paste("./aqs_data/hourly_", parameter, "_", year, ".zip", sep = ""), method = "libcurl")
  
  zip_file <- paste("./aqs_data/hourly_", parameter, "_", year, ".zip", sep = "")
  
  out_dir <-"./aqs_data"
  
  unzip(zip_file,exdir = out_dir)
  
  # creating data set
  
  aqs_data <- data.table::fread(Sys.glob(paste("./aqs_data/?ourly_", parameter, "_", year, ".csv", sep = "")), data.table = FALSE) %>% rename(state_FIPS = `State Code`,
                                                                                                                                              county_FIPS = `County Code`,
                                                                                                                                              site_number = `Site Num`,
                                                                                                                                              parameter = `Parameter Code`,
                                                                                                                                              date_gmt = `Date GMT`,
                                                                                                                                              hour_gmt = `Time GMT`,
                                                                                                                                              value = `Sample Measurement`) %>%
    mutate(state_FIPS = str_pad(state_FIPS, 2, "0", side = "left"),
           county_FIPS = str_pad(county_FIPS, 3, "0", side = "left"),
           FIPS_code = paste(state_FIPS, county_FIPS, sep = ""),
           value = as.numeric(value)) %>%
    filter(FIPS_code %in% FIPS_codes) %>% 
    select(state_FIPS, county_FIPS, site_number, parameter, date_gmt, hour_gmt, value) %>%
    filter(!is.na(value) & parameter %in% parameter_codes) %>% 
    mutate_all(as.character) %>% 
    mutate(parameter = ifelse(parameter == "88101", "pm25", parameter),
           parameter = ifelse(parameter == "88502", "pm25_local", parameter),
           parameter = ifelse(parameter == "42101", "carbon_monoxide", parameter),
           parameter = ifelse(parameter == "44201", "ozone", parameter),
           parameter = ifelse(parameter == "64101", "barometric_pressure", parameter))%>%
    group_by(state_FIPS, county_FIPS, date_gmt, hour_gmt, parameter, site_number) %>%
    filter(row_number()==1) %>% 
    mutate(value = as.numeric(value)) %>%
    ungroup()
  
  
  # saving file to data directory
  
  if (save_file){
    unlink("./aqs_data/*.zip")
  }else{
    unlink("./aqs_data/*")
  }
  
  # close file connections
  closeAllConnections()
  
  return(aqs_data)
}

